package com.qfjy.project.meeting;
import com.qfjy.entity.MeetingPub;
import com.qfjy.entity.MeetingGrab;
import com.qfjy.entity.User;
import com.qfjy.service.MeetingGrabService;
import com.qfjy.service.MeetingPubService;
import com.qfjy.uitl.ResultCode;
import com.qfjy.uitl.ResultJson;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("MeetingGrabController")
@Slf4j
@Api(tags = {"会议的抢单"})
public class MeetingGrabController {

    @Autowired
    private MeetingPubService meetingPubService;


    @Autowired
    private MeetingGrabService meetingGrabService;

    @ResponseBody
    @RequestMapping("list")//MeetingGrabController/list

    public ResultJson selectMeetingGrabListByUid(@RequestParam("uid") String uid, @RequestParam("tid") String tid) {

        List<MeetingPub> meetingPubs = meetingPubService.selectListMeetingGrabListByUid(uid, tid);

        if (meetingPubs == null) {

            return new ResultJson(null, ResultCode.NOT_DATA);

        }
        return new ResultJson(meetingPubs, ResultCode.SUCCESS);

    }

    @GetMapping("meetingGrabAddToPage")
    public String meetingGrabAddToPage(@RequestParam("uid") String uid, @RequestParam("pid") String pid, HttpServletRequest request) {

        request.setAttribute("uid", uid);

        request.setAttribute("pid", pid);

        return "pages/meetingGrab/meetingGrabAdd";

    }

    /**
     * 可抢单列表
     */
    @RequestMapping("meetingGrabAdd")

    @ResponseBody

    public ResultJson meetingGrabAdd(MeetingGrab meetingGrab, HttpSession session, String num1) {

        System.out.println(meetingGrab);

        session.setAttribute("uuid", num1);

        int num = 0;

        try {

            num = meetingGrabService.insert(meetingGrab);

            log.info("添加抢单信息成功");

            return new ResultJson(num, ResultCode.SUCCESS);

        } catch (Exception e) {

            e.printStackTrace();

            log.info("添加抢单信息失败");

            return new ResultJson(null, ResultCode.FAIL);

        }
    }

    @ResponseBody
    @RequestMapping("meetingGrabList")

    public ResultJson meetingGrabList(String uid) {



        List<MeetingGrab> meetingGrabs = meetingGrabService.selectMeetingGrabListByUid(uid);

//        for (MeetingGrab meetingGrab : meetingGrabs) {
//
//            System.out.println(meetingGrab);
//
//        }
        if (meetingGrabs.size() < 1) {

            return new ResultJson(null, ResultCode.NOT_DATA);

        }
        return new ResultJson(meetingGrabs, ResultCode.SUCCESS);

    }


    /**
     * 抢单者信息
     */
    @ResponseBody
    @RequestMapping("getGraListInfo")
     public ResultJson getGraListInfo(String pid){
        System.out.println("pid"+pid);
        List<User> users = meetingGrabService.selectUserInfoByUid(pid);
        System.out.println(users);
        System.out.println("已进入已进入已进入已进入已进入已进入已进入已进入已进入已进入已进入已进入");
        if(users==null){
            return  new ResultJson(null,ResultCode.NOT_DATA);

        }

        return new ResultJson(users,ResultCode.SUCCESS);

     }

}